package gui;

import java.awt.*;
import java.util.ArrayList;

import javax.swing.*;

import Projektarbete.User;

/*
 * Denna klassen hanterar listan med alla medlemmar i chatten.
 */

public class ChatMemberPanel extends JPanel{
	private ArrayList<User> userList = new ArrayList<User>();
	private JLabel header;
	private ArrayList<JLabel> labelList = new ArrayList<JLabel>();
	JPanel headerPanel = new JPanel();
	JPanel memberPanel = new JPanel();
	JPanel namePanel = new JPanel();
	public ChatMemberPanel(ArrayList<User> userList){
		
		headerPanel.setPreferredSize(new Dimension(100, 30));
		headerPanel.setMaximumSize(new Dimension(100, 30));
		headerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		headerPanel.setOpaque(false);
		memberPanel.setOpaque(false);
		namePanel.setOpaque(false);
		memberPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		header = new JLabel("Members in chat");
		headerPanel.add(header, BorderLayout.NORTH);
		add(headerPanel);
		add(memberPanel);
		memberPanel.add(namePanel);
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.Y_AXIS));
		this.userList = userList;
		for(int i = 0; i < userList.size(); i++){
			
			labelList.add(new JLabel(userList.get(i).getUserName()));
			namePanel.add(labelList.get(i));
		}
	}
	
	public ChatMemberPanel(ArrayList<User> userList, boolean group){
		
		headerPanel.setPreferredSize(new Dimension(100, 30));
		headerPanel.setMaximumSize(new Dimension(100, 30));
		headerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		headerPanel.setOpaque(false);
		memberPanel.setOpaque(false);
		namePanel.setOpaque(false);
		memberPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		header = new JLabel("Members in group");
		headerPanel.add(header, BorderLayout.NORTH);
		add(headerPanel);
		add(memberPanel);
		memberPanel.add(namePanel);
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.Y_AXIS));
		this.userList = userList;
		for(int i = 0; i < userList.size(); i++){
			
			labelList.add(new JLabel(userList.get(i).getUserName()));
			namePanel.add(labelList.get(i));
		}
	}
	
	public void addContact(User user){
		labelList.add(new JLabel(user.getUserName()));
		namePanel.add(labelList.get(labelList.size()));
	}
	
	public void removeContact(User user){
		for(int i = 0; i < userList.size(); i++){
			if(userList.get(i).getU_id() == user.getU_id()){
				namePanel.remove(labelList.get(i));
			}
		}
	}
	

}
